package com.mark.learn.mybatis.intercept;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;

import java.sql.Statement;
import java.util.Properties;

/**
 * @author Siyuan.mlq
 * @version 1.0
 * @description 自定义拦截器 type的可选类型有：Executor,ResultSetHandler,ParameterHandler,StatementHandler
 *     method对应type里的具体方法（不是所有方法）
 * @since 2018/8/30
 */
@Intercepts({
  @Signature(
      type = ResultSetHandler.class,
      method = "handleResultSets",
      args = {Statement.class})
})
@Slf4j
public class ResultSetHandlerInterceptorExample implements Interceptor {
  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    // 这里可以有很多的操作
    invocation.getTarget();
    invocation.getMethod();
    Object[] args = invocation.getArgs();
    // 相对于method.invoke(target, args)
    return invocation.proceed();
  }

  @Override
  public Object plugin(Object o) {
    return Plugin.wrap(o, this);
  }

  @Override
  public void setProperties(Properties properties) {
    // mybatis-config配置的plugin节点下的参数（变量），可以在这里通过properties获取。
    log.info(properties.propertyNames().toString());
    log.info(
        "name is :{},age is: {}", properties.getProperty("name"), properties.getProperty("age"));
  }
}
